import 'dart:convert';
import 'dart:io';
import 'dart:typed_data';

import 'package:crypto/crypto.dart';

class IpAddressResponse {
  IpAddressCodeInfo? ret;
  IpAddressData? data;

  IpAddressResponse({required this.ret, required this.data});

  IpAddressResponse.fromJson(Map<String, dynamic> json) {
    if( json['ret'] != null){
      ret = IpAddressCodeInfo.fromJson(json['ret']);
    }
    if( json['data'] != null){
      data = IpAddressData.fromJson(json['data']);
    }
  }

  Map<String, dynamic> toJson() => {"ret": ret, "data": data};

  @override
  String toString() {
    return 'IpAddressResponse{ret: $ret, data: $data}';
  }
}

class IpAddressCodeInfo {
  String retCode;
  String retMsg;

  IpAddressCodeInfo(this.retCode, this.retMsg);

  factory IpAddressCodeInfo.fromJson(Map<String, dynamic> json) =>
      IpAddressCodeInfo(json['ret_code'], json['ret_msg']);

  Map<String, dynamic> toJson() {
    Map<String, dynamic> data = <String, dynamic>{};
    data['ret_code'] = retCode;
    data['ret_msg'] = retMsg;
    return data;
  }

  @override
  String toString() {
    return 'IpAddressCodeInfo{ret_code: $retCode, ret_msg: $retMsg}';
  }
}

class IpAddressData {
  String province; //省份
  String? city; //省辖市(可能为空)
  String? area; //区/县(可能为空)
  String detailedAddress; //详细地址
  String ip; //公网ip

  IpAddressData(
      this.province, this.city, this.area, this.detailedAddress, this.ip);

  @override
  String toString() {
    return 'IpAddressData{province: $province, area: $area, detailedAddress: $detailedAddress, ip: $ip, city: $city}';
  }

  factory IpAddressData.fromJson(Map<String, dynamic> json) => IpAddressData(
      json['province'],
      json['city'],
      json['area'],
      json['detailedAddress'],
      json['ip']);

  Map<String, dynamic> toJson() {
    Map<String, dynamic> data = <String, dynamic>{};
    data['province'] = province;
    data['city'] = city;
    data['area'] = area;
    data['detailedAddress'] = detailedAddress;
    data['ip'] = ip;
    return data;
  }
}

class IPAddressUtil {
  static String appID = 'wuyezhongkong230301';
  static String appKey = '10012a15a243c4dfc85f7492a6e9e9c0';

  static Future<IpAddressData?> getIPAddress() async {
    late IpAddressData? ipAddressData;
    int timestamp = DateTime.now().millisecondsSinceEpoch;
    String sign = md5Format('$appID$appKey$timestamp');
    var url =
        'https://ipaddress.aphaea.cn:8443/ip-address/ipAddress/getIp?appId=$appID&timestamp=$timestamp&sign=$sign';
    var client = HttpClient();
    var request = await client.getUrl(Uri.parse(url));
    var response = await request.close();
    await response.transform(utf8.decoder).forEach((line) {
      if (line.isNotEmpty && line.contains('ret_code')) {
        var json = jsonDecode(line);
        var ipAddressResponse = IpAddressResponse.fromJson(json);
        ipAddressData = ipAddressResponse.data;
      }
    });
    return ipAddressData;
  }

  static String md5Format(String data) {
    Uint8List content = const Utf8Encoder().convert(data);
    Digest digest = md5.convert(content);
    return digest.toString();
  }
}
